load(
    "@io_bazel_rules_docker//container:container.bzl",
    "container_image",
)
load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar")
load("@io_bazel_rules_container_rpm//rpm:rpm.bzl", "rpm_image")

rpm_image(
    name = "qemu-img-image",
    base = select({
        "@io_bazel_rules_go//go/platform:linux_ppc64le": "@fedora_ppc64le//image",
        "//conditions:default": "@fedora//image",
    }),
    rpms = select({
        "@io_bazel_rules_go//go/platform:linux_ppc64le": [
            "@qemu-img_ppc64le//file",
            "@capstone_ppc64le//file",
            "@libaio_ppc64le//file",
            "@libstdc_ppc64le//file",
        ],
        "//conditions:default": [
            "@qemu-img//file",
            "@capstone//file",
            "@libaio//file",
            "@libstdc//file",
        ],
    }),
)

genrule(
    name = "alpine-img",
    srcs = select({
        "@io_bazel_rules_go//go/platform:linux_ppc64le": ["@alpine_image_ppc64le//file"],
        "//conditions:default": ["@alpine_image//file"],
    }),
    outs = ["images/alpine/disk.img"],
    cmd = select({
        "@io_bazel_rules_go//go/platform:linux_ppc64le": "mkdir -p images/alpine && cat $(location @alpine_image_ppc64le//file) > $@",
        "//conditions:default": "mkdir -p images/alpine && cat $(location @alpine_image//file) > $@",
    }),
)

genrule(
    name = "cirros-img",
    srcs = select({
        "@io_bazel_rules_go//go/platform:linux_ppc64le": ["@cirros_image_ppc64le//file"],
        "//conditions:default": ["@cirros_image//file"],
    }),
    outs = ["images/cirros/disk.img"],
    cmd = select({
        "@io_bazel_rules_go//go/platform:linux_ppc64le": "mkdir -p images/cirros && cat $(location @cirros_image_ppc64le//file) > $@",
        "//conditions:default": "mkdir -p images/cirros && cat $(location @cirros_image//file) > $@",
    }),
)

genrule(
    name = "fedora-img",
    srcs = select({
        "@io_bazel_rules_go//go/platform:linux_ppc64le": ["@fedora_image_ppc64le//file"],
        "//conditions:default": ["@fedora_image//file"],
    }),
    outs = ["images/fedora-cloud/disk.qcow2"],
    cmd = select({
        "@io_bazel_rules_go//go/platform:linux_ppc64le": "mkdir -p images/fedora-cloud && cat $(location @fedora_image_ppc64le//file) > $@",
        "//conditions:default": "mkdir -p images/fedora-cloud && cat $(location @fedora_image//file) > $@",
    }),
)

genrule(
    name = "custom-img",
    srcs = [],
    outs = ["images/custom/disk.img"],
    cmd = "mkdir -p images/custom && truncate -s 64M disk.img && cat disk.img > $@",
)

pkg_tar(
    name = "alpine-tar",
    srcs = [
        ":alpine-img",
    ],
    package_dir = "images/alpine",
)

pkg_tar(
    name = "cirros-tar",
    srcs = [
        ":cirros-img",
    ],
    package_dir = "images/cirros",
)

pkg_tar(
    name = "fedora-tar",
    srcs = [
        ":fedora-img",
    ],
    package_dir = "images/fedora-cloud",
)

pkg_tar(
    name = "custom-tar",
    srcs = [
        ":custom-img",
    ],
    package_dir = "images/custom",
)

container_image(
    name = "disks-images-provider-image",
    architecture = select({
        "@io_bazel_rules_go//go/platform:linux_ppc64le": "ppc64le",
        "//conditions:default": "amd64",
    }),
    base = ":qemu-img-image",
    directory = "/",
    entrypoint = ["/entrypoint.sh"],
    files = ["entrypoint.sh"],
    tars = [
        ":alpine-tar",
        ":cirros-tar",
        ":fedora-tar",
        ":custom-tar",
    ],
    visibility = ["//visibility:public"],
)
